var viewer;

function app(option) {
  this.viewer = new Cesium.Viewer(option.domId, {
    scene3DOnly: true,
    selectionIndicator: false,
    baseLayerPicker: false,
    animation: false,
    timeline: false,
    navigationHelpButton: false,
    fullscreenButton: true,
    geocoder: true,
    homeButton: true,
    shouldAnimate: false,
    sceneModePicker: false,
    shouldAnimate: false,
    infoBox: false,
    requestRenderMode: false,
    navigationInstructionsInitiallyVisible: false,
    baseLayerPicker: false,
    contextOptions: {
      webgl: {
        alpha: true,
        depth: true,
        stencil: true,
        antialias: true,
        premultipliedAlpha: true,
        preserveDrawingBuffer: true,
        failIfMajorPerformanceCaveat: true
      }
    },
/*    imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
      url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
    }), */
  });
  this.viewer.scene.globe.depthTestAgainstTerrain = false;
  this.viewer.cesiumWidget.creditContainer.style.display = "none";

  //设置操作习惯(禁止中间滑轮调整视角)
  this.viewer.scene.screenSpaceCameraController.zoomEventTypes = [Cesium.CameraEventType.WHEEL, Cesium.CameraEventType.PINCH];
  this.viewer.scene.screenSpaceCameraController.tiltEventTypes = [Cesium.CameraEventType.PINCH, Cesium.CameraEventType.RIGHT_DRAG];

  //解决模糊问题
  if (Cesium.FeatureDetection.supportsImageRenderingPixelated()) { //判断是否支持图像渲染像素化处理
    this.viewer.resolutionScale = window.devicePixelRatio;
  }
  //是否开启抗锯齿
  this.viewer.scene.fxaa = true;
  this.viewer.scene.postProcessStages.fxaa.enabled = true;

  // 去掉entity的点击事件 start
  this.viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
  this.viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
  this.viewer.scene.globe.baseColor = new Cesium.Color.fromCssColorString("#546a53"); //地表背景色
  this.viewer.scene.debugShowFramesPerSecond = false;
  viewer = this.viewer;
}

function addGltf(option) {
/*  var model = viewer.scene.primitives.add(Cesium.Model.fromGltf(
    {
      id: option.id,
      name: option.name || '未命名',
      url: option.url,
      type: option.type,
      code: option.code || '',
      show: option.true,
      modelMatrix: this.getGltfMatrix(option),
      scale: option.scale * 1 || 1,
      minimumPixelSize: 128,
      maximumScale: 2000,
      allowPicking: true,
      debugShowBoundingVolume: false,
      debugWireframe: false,
      backFaceCulling: false,
    })); */

    var modleGlb = viewer.entities.add({
        position:  Cesium.Cartesian3.fromDegrees(option.position.x*1, option.position.y*1,option.position.z*1),
        id: option.id,
        name: option.name || '未命名',
        url: option.url,
        type: option.type,
        code: option.code || '',
        show: option.true,
        model: {
            uri: option.url
        }
    });
    return modleGlb;
}

function getGltfMatrix(option){
	  var heading = Cesium.defaultValue(option.rotation.y, 0.0);
    var pitch = Cesium.defaultValue(option.rotation.x, 0.0);
    var roll = Cesium.defaultValue(option.rotation.z, 0.0);
    var headingPitchRoll = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(heading), Cesium.Math.toRadians(pitch), Cesium.Math.toRadians(roll));
    var origin = Cesium.Cartesian3.fromDegrees(option.position.x*1, option.position.y*1,option.position.z*1);
	return Cesium.Transforms.headingPitchRollToFixedFrame(origin, headingPitchRoll, Cesium.Ellipsoid.WGS84, Cesium.Transforms.eastNorthUpToFixedFrame, new Cesium.Matrix4());
}

function getProperty(paths, times) {
    var property = new Cesium.SampledPositionProperty();
    var lineLength = paths.length;
    var tempTime = times - times % distance;
    var increment = tempTime / lineLength;
    var start = Cesium.JulianDate.now()
    this.start = start;
    var stop = Cesium.JulianDate.addSeconds(start, tempTime, new Cesium.JulianDate());
    this.stop = stop;
    this.viewer.clock.startTime = start.clone();
    this.viewer.clock.stopTime = stop.clone();
    this.viewer.clock.currentTime = start.clone();
    this.viewer.clock.clockRange =this.clockRange; // Loop at the end
    this.viewer.clock.multiplier = this.speed;
  //  this.viewer.clock.onStop.addEventListener(this.options.complete);
    for ( i = 0; i < lineLength; i++) {
        var time = Cesium.JulianDate.addSeconds(start, i * increment, new Cesium.JulianDate());
        property.addSample(time,paths[i]);
    }
    return property;
}

   function disTance(a,b){
       var satrt = Cesium.Cartographic.fromDegrees(a.x,a.y)
       var end = Cesium.Cartographic.fromDegrees(b.x,b.y)
       var geodesic = new Cesium.EllipsoidGeodesic();
       geodesic.setEndPoints(satrt, end);
       var distance = geodesic.surfaceDistance
       return distance
  }
